package com.xiashitech.apiservice.job;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xiashitech.apiservice.dao.model.AgentNode;
import com.xiashitech.apiservice.dao.model.PushHistory;
import com.xiashitech.apiservice.dto.AgentIdentityDTO;
import com.xiashitech.apiservice.grpc.IncrementalCacheService;
import com.xiashitech.apiservice.job.base.XSQuartzJobBean;
import com.xiashitech.apiservice.service.IPushHistroyService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class PushRetry {
    @Component
    class PushRetryJob extends XSQuartzJobBean {
        private final IncrementalCacheService incrementalCacheService;
        private IPushHistroyService pushHistroyService;

        @Autowired
        public PushRetryJob(IncrementalCacheService incrementalCacheService, IPushHistroyService pushHistroyService) {
            this.incrementalCacheService = incrementalCacheService;
            this.pushHistroyService = pushHistroyService;
        }

        @Override
        protected void xsExecuteInternal(JobExecutionContext context) throws JobExecutionException {
           // System.out.println("push retry Job execute");
           List<AgentIdentityDTO> currenAgentList= incrementalCacheService.getAllAgentIds();
            currenAgentList.forEach(agentIdentityDTO -> {
              PushHistory queryPushHistory = new PushHistory();
                queryPushHistory.setSystemId(agentIdentityDTO.getSystemId());
                queryPushHistory.setServiceId(agentIdentityDTO.getServiceId());
                queryPushHistory.setSerialId(agentIdentityDTO.getSerialId());
                List<PushHistory> historyList = pushHistroyService.selectRetryHistory(queryPushHistory);
                historyList.forEach(pushHistory -> {
                    JSONObject pushObj = JSONUtil.parseObj(pushHistory.getContent());
                    pushObj.putOpt("pushId",pushHistory.getId());
                    incrementalCacheService.pushIncrementToClient((JSONUtil.parseObj(pushHistory.getContent()).get("systemId").toString()), JSONUtil.toJsonStr(pushObj));
                });
            });

        }
    }
    @Bean("push_retry_job")
    public JobDetail jobDetail() {
        JobDetail jobDetail = JobBuilder.newJob(PushRetryJob.class)
                .withIdentity("push_retry_job", "push_retry_job")
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean("push_retry_trigger")
    public Trigger trigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(jobDetail())
                .withIdentity("push_retry_job", "push_retry_job")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("* 0/1 * * * ?"))
                .build();
        return trigger;
    }

}
